<?php
class home extends core {
	public function __construct() {
		parent::__construct();
	}

	//index
	public function index() {
		$this->display('home.index.html');
	}

	//login
	public function login() {
		if (IS_POST) {
			$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
			$password = isset($_POST['password']) ?  $_POST['password'] : '';
			if ($name =='') error('账户不能为空');
			if ($password =='') error('密码不能为空');
			$sql = "SELECT * FROM jf_beautyshop WHERE login_name='{$name}'";
			$row = $this->db->get_row($sql);
			if ($row) {
				$crypt_password = crypt_password($password, $row->salt);
				if ($crypt_password == $row->password) {
					$this->shopadmin_id = $row->id;
					$this->shopadmin_name = $row->name;
					$_SESSION['shopadmin'] = $row;
					$sql = "UPDATE jf_beautyshop SET last_time='".time()."', last_ip='".ip()."', logins=logins+1 WHERE id='{$row->id}'";
					$this->db->query($sql);
					$_SESSION['shopadmin']->last_ip = ip();
					$_SESSION['shopadmin']->last_time = date("Y-m-d H:i:s",time());
					header("Location: ?app=home&act=index");
					exit;
					//$this->display('home.index.html');
				} else {
					exit('Username/Password Is Unvalid.');
				}
			} else {
				exit('Login Failed');
				//$this->error('Login failed');
			}
		} else {
			$this->display();
		}
	}

	public function logout(){
		session_unset();
		if (isset($_SESSION['shopadmin'])) unset($_SESSION['shopadmin']);
		if ($this->shopadmin_id>0) $this->shopadmin_id = 0;		
		header("Location: ?app=home&act=login");
	}
}
